package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ClientBean;
import com.project.bean.CustomerBean;
import com.project.bean.SaleBean;
import com.project.bean.SaleCarBean;
import com.project.mapper.*;
import com.project.service.ISaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class SaleServiceImpl implements ISaleService {
    @Autowired
    private ISaleMapper saleMapper;
    @Autowired
    private ISaleInfoMapper infoMapper;
    @Autowired
    private ISaleCarMapper carMapper;
    @Autowired
    private IGoodsMapper goodsMapper;
    @Autowired
    private ICustomerMapper clientMapper;

    @Override
    public void add(Integer userId, SaleBean saleBean) {
             saleBean.setTotalPrice(carMapper.getTotalPrice(userId));
             saleMapper.insert(saleBean);
        QueryWrapper<SaleCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        List<SaleCarBean> sellCarList = carMapper.findUserId(userId);
        infoMapper.addInfo(saleBean.getId(),sellCarList);
        goodsMapper.updateSle(sellCarList);

        carMapper.delete(qw);

    }

    @Override
    public IPage<SaleBean> findByItem(Integer pageNo, String clientName, LocalDate startDate, LocalDate endDate) {
        return saleMapper.findByItem(new Page(pageNo,5),clientName,startDate,endDate);
    }


    @Override
    public SaleBean findByID(Integer id) {
        SaleBean saleBean= saleMapper.selectById(id);
        saleBean.setSaleInfoList(infoMapper.findList(id));
        saleBean.setCustomerBean(clientMapper.findBySaleId(id));
        return saleBean;
    }
}
